# -*- coding: utf-8 -*-
import scrapy
from tc58.items import Tc58Item


class TongchengSpider(scrapy.Spider):
    name = 'tongcheng'
    allowed_domains = ['58.com']
    start_urls = ['https://bj.58.com/job.shtml?utm_source=sem-baidu-pc&spm=105916147073.26420796295&PGTID=0d100000-0000-17a3-1822-398773a86f3a&ClickID=2']

    def parse(self, response):
        # with open('tc.html','wb') as f:
        #     f.write(response.body)
        hrefs = response.xpath('//div[@class="sidebar-right fl"]//a/@href').extract()
        for href in hrefs:
            href = 'https://bj.58.com'+href
            yield scrapy.Request(href,callback=self.parse_all)
            # print(href)

    def parse_all(self,response):
        hrefs = response.xpath('//div[@class="job_name clearfix"]/a/@href').extract()
        for href in hrefs:
            yield scrapy.Request(href,callback=self.parse_one_page_info)



    def parse_one_page_info(self,response):
        title = response.xpath('//span[@class="pos_title"]/text()').extract_first()
        # print(title)
        price = response.xpath('//span[@class="pos_salary"]/text()').extract_first()
        # print(price)
        weizhi = response.xpath('//div[@class="pos-area"]/span[2]/text()').extract_first()
        # print(weizhi)
        jingyan = response.xpath('//div[@class="pos_base_condition"]/span[3]/text()').extract_first()
        # print(jingyan)
        xueli = response.xpath('//div[@class="pos_base_condition"]/span[2]/text()').extract_first()
        # print(xueli)
        shijian = response.xpath('//span[@class="pos_base_num pos_base_update"]/text()').extract_first()
        # print(shijian)
        info = response.xpath('//div[@class="des"]//text()').extract()
        info = ''.join(info)
        # print(info)
        wangzhan = '同城'
        # print(wangzhan)
        print('='*60)

        item = Tc58Item()
        item['title'] =title
        item['price'] = price
        item['weizhi'] = weizhi
        item['jingyan'] =jingyan
        item['xueli'] =xueli
        item['shijian']=shijian
        item['info']=info
        item['wangzhan']= wangzhan
        yield item

















